/***************************************************************************
 *   Copyleft 2008-2009 by												   *
 *	 Iwanicki Krzysztof													   *
 *   <gamma.mini.pw.edu.pl/~iwanickik>									   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 ***************************************************************************/

#ifdef V_WIN32_PLATFORM

#include "./vWin32GlExtensionManager.h"
#include "../../Macros/vLogMacros.h"
#include "Gl/glew.h"
#include "Gl/wglew.h"
#include  "../../Macros/vGlMacros.h"

namespace Vendetta {
	// --------------------------------------------------
	//	Construct
	// --------------------------------------------------

	vWin32GlExtensionManager::vWin32GlExtensionManager(): vGlExtensionManager() {
		mExtensions<<vPair<vString, vBool>("WGL_EXT_swap_control", false);
	}

	// --------------------------------------------------
	//	Destruct
	// --------------------------------------------------

	vWin32GlExtensionManager::~vWin32GlExtensionManager() {

	}

	// --------------------------------------------------
	//	supports
	// --------------------------------------------------

	vBool vWin32GlExtensionManager::Supports(const vString& GLExtension) {
		return vGlExtensionManager::Supports(GLExtension) || wglewIsSupported(GLExtension.ToConstCharPtr());
	}

	// --------------------------------------------------
	//	refresh
	// --------------------------------------------------

	vBool vWin32GlExtensionManager::Init() {
		GLenum err = glewInit();

		if (GLEW_OK != err)	{
			/* Problem: glewInit failed, something is seriously wrong. */
			LOGS_ERROR("GLEW Init failed.");
			return false;
		}
		LOGS_INFO("  GLEW Version: %s", glewGetString(GLEW_VERSION));
		LOGS_INFO("  Checking for extension support:");

		vPair<vString, vBool> ext;
		vString spaces;
		vUInt uiSpaceLeft = 0;
		for (vUInt uiIndx = 0; uiIndx < mExtensions.Size(); uiIndx++) {
			uiSpaceLeft = 0;
			spaces = "";
			ext = mExtensions.Element(uiIndx);
			mExtensions[ext.First] = Supports(ext.First.ToConstCharPtr()) ? true : false;
			while (uiSpaceLeft++ < 58 - ext.First.Length()) {
				spaces<<" ";
			}
			if (mExtensions[ext.First]) {
				LOGS_INFO("    %s%s[OK]", ext.First.ToConstCharPtr(), spaces.ToConstCharPtr());
			} else {
				LOGS_ERROR("    %s%s[FAILED]", ext.First.ToConstCharPtr(), spaces.ToConstCharPtr());
				return false;
			}
		}

		return true;
	}
}

#endif // V_WIN32_PLATFORM